import 'dart:io';
import 'dart:async';
import 'package:path/path.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart';

class Generator {
  Generator({this.domain, this.output, this.max});
  String domain;
  String output;
  String nextUrl;
  int max;
  int i = 0; // 打开页面数
  int count = 0; // 获取链接数
  Duration duration;

  String content = '''
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
''';
  String suffix = '''
</urlset>
''';

  Future<bool> generate() async {
    DateTime start = new DateTime.now();
    await parser(domain);
    output = absolute(normalize(output));
    File sitemap = new File(output);
    if (!sitemap.existsSync()) {
      sitemap.createSync(recursive: true);
    }
    sitemap.writeAsStringSync(content);
    duration = new DateTime.now().difference(start);
    return true;
  }

  parser(String url) async {
    http.Response response;
    Document body;
    List<String> urls;

    loop:
    for (; i < max; i++) {
      if (null != nextUrl) {
        url = nextUrl;
      }
      if (null == url && null == nextUrl) {
        break loop;
      }
      try {
        stdout.write("抓取第${i + 1}页，url=${url}");
        response = await http.get(url);
      } catch (e) {
        print('读取网页失败 $e');
        return;
      }
      body = parse(response.body);
      List<Element> urlEles = body.querySelectorAll('.card>a');
      if (null == urlEles || urlEles.isEmpty) {
        stdout.writeln('第${i+1}页无链接，url=${url}');
      }
      urls = urlEles.map((e) => domain + e.attributes['href']).toList();
      stdout.writeln(' 有${urls.length}个链接');
      count += urls.length;
      urls.forEach((url) {
        content += getSection(url, priority: 0.8);
      });
      Element a =
          body.querySelectorAll('.pagination>li').last.querySelector('a');
      if (null == a) {
        break loop;
      } else {
        nextUrl = domain + a.attributes['href'];
      }
    }
    content += suffix;
  }

  String getSection(String url, {double priority: 0.5, String lastmod}) {
    return '''
  <url>
    <loc>$url</loc>${null != lastmod
        ? '\n<lastmod>2018-05-14T07:40:20+00:00</lastmod>'
        : ''}
    <priority>$priority</priority>
  </url>
''';
  }

  @override
  String toString() {
    return '共打开${i}个页面，获取${count}个链接，耗时${duration.inMilliseconds/1000}秒'
        '，文件输出在${output}';
  }
}
